/**
 * 重写CategoryAxis组件的一些方法, 实现时间坐标轴只显示能被5整除的分钟数. 仅应用于指定图表.
 */
define(
	[
		'require',
		'echarts',
		'echarts/config',
		'echarts/chart/line',
		'zrender/config',
		'zrender/tool/util',
		'zrender/tool/color',
		'zrender/shape/Text',
		'zrender/shape/Line',
		'zrender/shape/Rectangle',
		'zrender/tool/area',
		'echarts/component/categoryAxis'
	],
	function (require) {
		"use strict";

		var ecConfig = require('echarts/config');
		var zrConfig = require('zrender/config');
		var zrEvent = require('zrender/tool/event');
		var zrUtil = require('zrender/tool/util');
		var zrArea = require('zrender/tool/area');
		var zrToolColor = require('zrender/tool/color');
		var TextShape = require('zrender/shape/Text');
		var LineShape = require('zrender/shape/Line');
		var RectangleShape = require('zrender/shape/Rectangle');


		//应用此改动的图表id列表
		var _applyTo = Ember.A();
		var CategoryAxis = require('echarts/component/categoryAxis');
		var base_buildSplitLine = CategoryAxis.prototype._buildSplitLine;
		var base_buildSplitArea = CategoryAxis.prototype._buildSplitArea;

		// 分隔线
		var _buildSplitLine = function () {
			var axShape;
			var data		= this.option.data;
			var dataLength  = this.option.data.length;
			var sLineOption = this.option.splitLine;
			var labelOption = this.option.axisLabel;
			var lineType	= sLineOption.lineStyle.type;
			var lineWidth   = sLineOption.lineStyle.width;
			var color	   = sLineOption.lineStyle.color;
			color = color instanceof Array ? color : [color];
			var colorLength = color.length;

			var intervalFunction = typeof sLineOption.interval == 'function'
								   ? sLineOption.interval 
								   : sLineOption.interval == undefined
									 ? typeof labelOption.interval == 'function'
									   ? labelOption.interval : false
									 : false;
			var interval   = intervalFunction
							 ? 1
							 : sLineOption.interval == 'auto' 
							   ? this._interval
							   : (sLineOption.interval - 0 + 1);
			var onGap	  = sLineOption.onGap;
			var optGap	 = onGap 
							 ? (this.getGap() / 2) 
							 : typeof onGap == 'undefined'
								   ? (this.option.boundaryGap ? (this.getGap() / 2) : 0)
								   : 0;
			var startIndex = optGap > 0 ? -interval : 0;
			dataLength -= (onGap || (typeof onGap == 'undefined' && this.option.boundaryGap)) 
						  ? 1 : 0;
			if (this.isHorizontal()) {
				// 横向
				var sy = this.grid.getY();
				var ey = this.grid.getYend();
				var x;

				for (var i = startIndex; i < dataLength; i += interval) {
					if(i === 0) { continue; }
					if (intervalFunction && !intervalFunction(i, data[i])) {
						// 回调并且回调返回false则跳过渲染
						continue;
					}
					// 亚像素优化
					x = this.subPixelOptimize(
						this.getCoordByIndex(i) + optGap, lineWidth
					);
					axShape = {
						// shape : 'line',
						zlevel: this.getZlevelBase(),
						z: this.getZBase(),
						hoverable : false,
						style : {
							xStart : x,
							yStart : sy,
							xEnd : x,
							yEnd : ey,
							strokeColor : color[(i / interval) % colorLength],
							lineType : lineType,
							lineWidth : lineWidth
						}
					};
					this.shapeList.push(new LineShape(axShape));
				}

			}
			else {
				// 纵向
				var sx = this.grid.getX();
				var ex = this.grid.getXend();
				var y;

				for (var i = 0; i < dataLength; i += interval) {
					if (intervalFunction && !intervalFunction(i, data[i])) {
						// 回调并且回调返回false则跳过渲染
						continue;
					}
					// 亚像素优化
					y = this.subPixelOptimize(
						this.getCoordByIndex(i) - optGap, lineWidth
					);
					axShape = {
						// shape : 'line',
						zlevel: this.getZlevelBase(),
						z: this.getZBase(),
						hoverable : false,
						style : {
							xStart : sx,
							yStart : y,
							xEnd : ex,
							yEnd : y,
							strokeColor : color[(i / interval) % colorLength],
							lineType : lineType,
							lineWidth : lineWidth
						}
					};
					this.shapeList.push(new LineShape(axShape));
				}
			}
		};

		//分隔区域
		var _buildSplitArea = function () {
			var axShape;
			var data		= this.option.data;
			var sAreaOption = this.option.splitArea;
			var labelOption = this.option.axisLabel;
			var color = sAreaOption.areaStyle.color;
			if (!(color instanceof Array)) {
				// 非数组一律认为是单一颜色的字符串，单一颜色则用一个背景，颜色错误不负责啊！！！
				axShape = {
					// shape : 'rectangle',
					zlevel: this.getZlevelBase(),
					z: this.getZBase(),
					hoverable : false,
					style : {
						x : this.grid.getX(),
						y : this.grid.getY(),
						width : this.grid.getWidth(),
						height : this.grid.getHeight(),
						color : color
						// type : this.option.splitArea.areaStyle.type,
					}
				};
				this.shapeList.push(new RectangleShape(axShape));
			}
			else {
				// 多颜色
				var colorLength = color.length;
				var dataLength  = this.option.data.length;

				var intervalFunction = typeof sAreaOption.interval == 'function'
								   ? sAreaOption.interval 
								   : sAreaOption.interval == undefined
									 ? typeof labelOption.interval == 'function'
									   ? labelOption.interval : false
									 : false;
				var interval   = intervalFunction
								 ? 1
								 : sAreaOption.interval == 'auto' 
								   ? this._interval
								   : (sAreaOption.interval - 0 + 1);
				var onGap	  = sAreaOption.onGap;
				var optGap	 = onGap 
								 ? (this.getGap() / 2) 
								 : typeof onGap == 'undefined'
									   ? (this.option.boundaryGap ? (this.getGap() / 2) : 0)
									   : 0;
				var startIndex = optGap > 0 ? -interval : 0;
				if (this.isHorizontal()) {
					// 横向
					var y = this.grid.getY();
					var height = this.grid.getHeight();
					var lastX = this.grid.getX();
					var curX;
	
					for (var i = startIndex; i <= dataLength; i += interval) {
						if (intervalFunction && !intervalFunction(i, data[i]) && i < dataLength) {
							// 回调并且回调返回false则跳过渲染
							continue;
						}
						curX = i < dataLength
							   ? (this.getCoordByIndex(i) + optGap)
							   : this.grid.getXend();
						axShape = {
							// shape : 'rectangle',
							zlevel: this.getZlevelBase(),
							z: this.getZBase(),
							hoverable : false,
							style : {
								x : lastX,
								y : y,
								width : curX - lastX,
								height : height,
								color : color[(i / interval) % colorLength]
								// type : this.option.splitArea.areaStyle.type,
							}
						};
						this.shapeList.push(new RectangleShape(axShape));
						lastX = curX;
					}
				}
				else {
					// 纵向
					var x = this.grid.getX();
					var width = this.grid.getWidth();
					var lastYend = this.grid.getYend();
					var curY;
	
					for (var i = 0; i <= dataLength; i += interval) {
						if (intervalFunction && !intervalFunction(i, data[i]) && i < dataLength) {
							// 回调并且回调返回false则跳过渲染
							continue;
						}
						curY = i < dataLength
							   ? (this.getCoordByIndex(i) - optGap)
							   : this.grid.getY();
						axShape = {
							// shape : 'rectangle',
							zlevel: this.getZlevelBase(),
							z: this.getZBase(),
							hoverable : false,
							style : {
								x : x,
								y : curY,
								width : width,
								height : lastYend - curY,
								color : color[(i / interval) % colorLength]
								// type : this.option.splitArea.areaStyle.type
							}
						};
						this.shapeList.push(new RectangleShape(axShape));
						lastYend = curY;
					}
				}
			}
		};

		CategoryAxis.prototype._buildSplitLine = function(){
			//_applyTo列表中的图表才用重写的方法
			if(_applyTo.contains(this.myChart.id)){
				_buildSplitLine.apply(this, arguments);
			}else{
				base_buildSplitLine.apply(this, arguments);
			}
		};

		CategoryAxis.prototype._buildSplitArea = function(){
			//_applyTo列表中的图表才用重写的方法
			if(_applyTo.contains(this.myChart.id)){
				_buildSplitArea.apply(this, arguments);
			}else{
				base_buildSplitArea.apply(this, arguments);
			}
		};
	
		return {
			/**
			 	添加应用该扩展的图表
			 	@param {string|number} id 使用该扩展的echart图表的id
			 */
			applyTo: function(id){
				_applyTo.push(id);
			},

			/**
			 	从应用该扩展的图表列表中移除指定图表
			 	@param {string|number} id echart图表的id
			 */
			unApplyTo: function(id){
				_applyTo.removeObject(id);
			}
		};
	}
);